/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package write;

import data.ACStorage;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.io.PrintWriter;

/**
 *
 * @author Ivan
 */
public class HtmlWriter {
    public static void writeIntoHtml(String filepath) throws IOException {
        BufferedWriter bw = new BufferedWriter(new FileWriter(new File(filepath)));
        PrintWriter pw = new PrintWriter(bw);
        pw.print("<!DOCTYPE html");
        pw.print("<html>");
        
        pw.print("<head>");
        
        pw.print("<title>Final results</title>");
        
        pw.print("<style>");
        pw.print("#newspaper-a{font-size: 16px;margin: 45px;text-align: left;border-collapse: collapse;border: 1px solid #69c;}");
        pw.print("#newspaper-a th{padding: 12px 17px 12px 17px;font-weight: normal;font-size: 18px;color: #039;border-bottom: 1px dashed #69c;text-align: center;}");
        pw.print("#newspaper-a td{font-size: 16px;padding: 7px 17px 7px 17px;color: #669;}");
        pw.print("#newspaper-a tbody tr:hover td{color: #339;background: #d0dafd;}");   
        pw.print("</style>");
        
        pw.print("</head>");
        
        pw.print("<h1 style=\"color:#03A; padding: 1em 0 0 3em;\">Final results</h1>");
        pw.print("<table id=" + "newspaper-a" + ">");
        pw.print("<th><strong>Goal</strong></th><th>description</th>");
        pw.print("<tr><td>" + ACStorage.getInstance().getGoal().getName() + "</td><td>" + ACStorage.getInstance().getGoal().getDescription()+"</td>");
        pw.print("</table>");
                
        pw.print("<table id=" + "newspaper-a" + ">");
        pw.print("<th colspan=2>Alternatives</th>");
        
        for (int i = 0; i < ACStorage.getInstance().getGoal().getListAlternative().size(); i++) {
            pw.print("<tr><td>");
            
            pw.print(ACStorage.getInstance().getGoal().getListAlternative().get(i).getName());
            pw.print("</td><td>");
            pw.print(ACStorage.getInstance().getGoal().getListAlternative().get(i).getDescription());
            pw.print("</td></tr>");
        }
        pw.print("</table>");

        pw.print("<table id=" + "newspaper-a" + ">");
        pw.print("<th colspan=2>Criterias</th>");
        
        for (int i = 0; i < ACStorage.getInstance().getGoal().getListCriteria().size(); i++) {
            pw.print("<tr><td>");
            pw.print(ACStorage.getInstance().getGoal().getListCriteria().get(i).getName());
            pw.print("</td><td>");
            pw.print(ACStorage.getInstance().getGoal().getListCriteria().get(i).getDescription());
            pw.print("</td></tr>");

        }
        pw.print("</table>");
        String a1 = ACStorage.getInstance().getGoal().getListAlternative().size() + 1 + "";
        String a2 = ACStorage.getInstance().getGoal().getListAlternative().size() + 2 + "";
        String c1 = ACStorage.getInstance().getGoal().getListCriteria().size() + 1 + "";
        String c2 = ACStorage.getInstance().getGoal().getListCriteria().size() + 2 + "";
        for (int x = 0; x < ACStorage.getInstance().getGoal().getListCriteria().size(); x++) {

            pw.print("<table id=" + "newspaper-a" + ">");
            pw.print("<th colspan="+a1+">Alternative marks</th>");
            pw.print("<tr><td>");
            pw.print(ACStorage.getInstance().getGoal().getListCriteria().get(x).getName());

            pw.print("</td>");
            for (int i = 0; i < ACStorage.getInstance().getGoal().getListAlternative().size(); i++) {
                pw.print("<td>");
                pw.print(ACStorage.getInstance().getGoal().getListAlternative().get(i).getName());
                pw.print("</td>");
            }
            pw.print("</tr>");

            for (int i = 0; i < ACStorage.getInstance().getGoal().getListAlternative().size(); i++) {
                pw.print("<tr><td>");
                pw.print(ACStorage.getInstance().getGoal().getListAlternative().get(i).getName());
                pw.print("</td>");
                for (int j = 0; j < ACStorage.getInstance().getGoal().getListAlternative().size(); j++) {
                    pw.print("<td>");
                    pw.print(ACStorage.getInstance().getGoal().getListCriteria().get(x).getAlternativeRankInList(ACStorage.getInstance().getGoal().getListAlternative().get(j), ACStorage.getInstance().getGoal().getListAlternative().get(i)).getMark());
                    pw.print("</td>");

                }
                pw.print("</tr>");
            }
            pw.print("</table>");    

            pw.print("<table id=" + "newspaper-a" + "><th colspan="+a2+">Alternative normalized marks</th>");
            pw.print("<tr><td>");
            
            pw.print(ACStorage.getInstance().getGoal().getListCriteria().get(x).getName());
            pw.print("</td>");

            for (int i = 0; i < ACStorage.getInstance().getGoal().getListAlternative().size(); i++) {
                pw.print("<td>");
                pw.print(ACStorage.getInstance().getGoal().getListAlternative().get(i).getName());
                pw.print("</td>");
            }
            pw.print("<td>");
            pw.print("Average values");
            pw.print("</td></tr>");
            for (int i = 0; i < ACStorage.getInstance().getGoal().getListAlternative().size(); i++) {
                pw.print("<tr><td>");
                pw.print(ACStorage.getInstance().getGoal().getListAlternative().get(i).getName());
                pw.print("</td>");
                for (int j = 0; j < ACStorage.getInstance().getGoal().getListAlternative().size(); j++) {
                    pw.print("<td>");
                    pw.print(ACStorage.getInstance().getGoal().getListCriteria().get(x).getAlternativeRankInList(ACStorage.getInstance().getGoal().getListAlternative().get(j), ACStorage.getInstance().getGoal().getListAlternative().get(i)).getNormalizedMark());
                    pw.print("</td>");
                }
                pw.print("<td>");
                pw.print(ACStorage.getInstance().getGoal().getListCriteria().get(x).getAverageValue().get(i));
                pw.print("</td></tr>");
            }
            pw.print("</table>");

        }

        pw.print("<table id=" + "newspaper-a" + ">");
        pw.print("<th colspan="+c1+">Criteria marks</th>");
        
        pw.print("<tr>");
        pw.print("<td>Criterias</td>");
        
        for (int i = 0; i < ACStorage.getInstance().getGoal().getListCriteria().size(); i++) {
            pw.print("<td>");
            pw.print(ACStorage.getInstance().getGoal().getListCriteria().get(i).getName());
            pw.print("</td>");
        }

        pw.print("</tr>");
        for (int i = 0; i < ACStorage.getInstance().getGoal().getListCriteria().size(); i++) {
            pw.print("<tr><td>");
            pw.print(ACStorage.getInstance().getGoal().getListCriteria().get(i).getName());
            pw.print("</td>");
            for (int j = 0; j < ACStorage.getInstance().getGoal().getListCriteria().size(); j++) {
                pw.print("<td>");
                pw.print(ACStorage.getInstance().getGoal().getCriteriaWeightInList(ACStorage.getInstance().getGoal().getListCriteria().get(j), ACStorage.getInstance().getGoal().getListCriteria().get(i)).getMark());
                pw.print("</td>");
            }
            pw.print("</tr>");
        }
        pw.print("</table>");

        pw.print("<table id=" + "newspaper-a" + ">");
        pw.print("<th colspan="+c2+">Criteria normalized marks</th>");

        pw.print("<tr><td>Criterias</td>");
        for (int i = 0; i < ACStorage.getInstance().getGoal().getListCriteria().size(); i++) {
            pw.print("<td>");
            pw.print(ACStorage.getInstance().getGoal().getListCriteria().get(i).getName());
            pw.print("</td>");
        }
        pw.print("<td>");
        pw.print("Criteria ponders</td></tr>");

        for (int i = 0; i < ACStorage.getInstance().getGoal().getListCriteria().size(); i++) {
            pw.print("<tr><td>");
            pw.print(ACStorage.getInstance().getGoal().getListCriteria().get(i).getName());
            pw.print("</td>");
            for (int j = 0; j < ACStorage.getInstance().getGoal().getListCriteria().size(); j++) {
                pw.print("<td>");
                pw.print(ACStorage.getInstance().getGoal().getCriteriaWeightInList(ACStorage.getInstance().getGoal().getListCriteria().get(j), ACStorage.getInstance().getGoal().getListCriteria().get(i)).getNormalizedMark());
                pw.print("</td>");
            }
            pw.print("<td>");
            pw.print(ACStorage.getInstance().getGoal().getCriteriaPonders().get(i));
            pw.print("</td></tr>");
        }

        pw.print("</table>");  
        
        pw.print("<table id=" + "newspaper-a" + ">");        
        pw.print("<th colspan="+c2+">Final results</th>");
        
        pw.print("<tr><td>");
        
        pw.print(ACStorage.getInstance().getGoal().getName());
        pw.print("</td>");
        for(int i = 0; i < ACStorage.getInstance().getGoal().getListCriteria().size(); i++){
            pw.print("<td>");
            pw.print(ACStorage.getInstance().getGoal().getListCriteria().get(i).getName());
            pw.print("</td>");
        }
        pw.print("<td>");
        pw.print("Final results");
        pw.print("</td></tr>");
        for(int i = 0; i < ACStorage.getInstance().getGoal().getListAlternative().size(); i++){
            pw.print("<tr><td>");
            pw.print(ACStorage.getInstance().getGoal().getListAlternative().get(i).getName());
            pw.print("</td>");
            for (int j = 0; j < ACStorage.getInstance().getGoal().getListCriteria().size(); j++){
                pw.print("<td>");
                pw.print(ACStorage.getInstance().getGoal().getListCriteria().get(j).getAverageValue().get(i));
                pw.print("</td>");
            }
            pw.print("<td><strong>");
            pw.print(ACStorage.getInstance().getGoal().getListAlternative().get(i).getMark());
            pw.print("</strong></td></tr>");
        }
        pw.print("<tr><td>Criteria ponders</td>");
        
        for( int i = 0; i < ACStorage.getInstance().getGoal().getCriteriaPonders().size(); i++){
            pw.print("<td>");
            pw.print(ACStorage.getInstance().getGoal().getCriteriaPonders().get(i));
            pw.print("</td>");
        }
        pw.print("<td></td></tr>");
        pw.print("</table>");
        
        pw.print("</html>");

        pw.flush();
        pw.close();
    }
}
